// ==================================================================================
// Shared Genomics Project MPI Codebase
// Version 1.0 30/04/2010
//
// (c) 2010 University of Manchester all rights reserved
//
// This file is distributed under the GNU General Public License, Version 2.  
// Please see the file COPYING.txt for more details
// ==================================================================================

#ifndef _OPTIONS_H_
#define _OPTIONS_H_

#include <stdlib.h>

#include "types.h"

#ifdef __cplusplus
extern "C" {
#endif

/*!
\file
\ingroup	gio
\brief		Environmental/Analysis Options 
\details
	The \ref selected_options structure contains tthe majority of the environmental/filepath
	options used by this codebase.<br>
	Options are normally set in a program by combining calls to \ref getopt() with
	the \ref selected_options structure to parse command-line parameters.<br>
	The majority of the numerical values are lifted straight from PLINK, using the same nomenclature for 
	the variable names.
*/

/*! \brief Maximum Filepath length */
#define FILE_PATH_LENGTH 256

/*! \brief Maximum token length when parsing a delimited string */
#define TOKEN_LENGTH 80

/*! 
\brief Default inclusion threshold for the Hardy-Weinburg Filter 
\see bfilter_snp_do_hwe()
*/
#define DEFAULT_HWE_LIMIT (double) 0.001

/*! 
\brief Default inclusion threshold for the Person Missingness %filter (MIND).
\details Default filter setting does not remove any people from a data-set.
\see bfilter_person_do_mind()
*/
#define DEFAULT_MIND (double) 1.0 // Typical filter value = 0.01

/*! 
\brief Default inclusion threshold for the SNP Missingness %filter (GENO).
\details Default filter setting does not remove any SNPs from a data-set.
\see bfilter_snp_do_maf_and_mind()
*/
#define DEFAULT_GENO (double) 1.0 // Typical filter value = 0.01

/*! 
\brief Default inclusion threshold for the SNP Major Allelic Frequency %filter (MAF).
\details Default filter setting does not remove any SNPs from a data-set.
\see bfilter_snp_do_maf_and_mind()
*/
#define DEFAULT_MAF (double) 0.01

/*! \brief Default number of permutations. */
#define DEFAULT_NPERM 10000

/*! \brief Default inclusion threshold for epistasis 'interesting results'. */
#define DEFAULT_EPI_ALPHA1 (double) 0.01

/*! \brief Wider inclusion threshold for epistasis when performing permuted calculations. */
#define DEFAULT_EPI_ALPHA2 (double) 0.01

/*! \brief Default Confidence interval */
#define DEFAULT_CI_LEVEL (double) 0.95

/*! \brief Default phenotype index */
#define DEFAULT_MPHENO 1

/*! \brief NIBHI Cluster size */
#define AVAILABLE_CORES 102

/*! \brief Default column name to 'sort' statistical output */
#define DEFAULT_SORT_COL_NAME "EMP1"

#define DEFAULT_DISP_SNP_WINDOW 10
#define DEFAULT_DISP_SNP_WINDOW_KB 1000000

/*! \brief Environmental/Analysis Options */
struct selected_options {
	/*! 
	\brief Flag if reading a genomic variant file
	\details default = FALSE
	*/
	BOOL isGvar;

	/*! 
	\brief Flag if read a genomic variant file
	\details default = FALSE
	*/
	BOOL loadGvar;

	/*! 
	\brief Flag if input map file just has 3 input tokens per line
	\details default = FALSE
	*/
	BOOL isMap3File;

	/*! 
	\brief Flag if base pair distances bound to locus in Centi-Morgan units
	\details default = FALSE
	\sa locus::bp
	*/
	BOOL isCmMap;

	/*!
	\brief Flag if an "memory" data-set is SNP-major 
	\details default = TRUE
	\deprecated Not used, PLINK artifact.
	*/
	BOOL isSnpMajor;

	/*! \brief Path to a text PED file of allelic data */
	char szPedFileName[FILE_PATH_LENGTH]; 

	/*! \brief Path to a text Map listing the SNPs in a genomic data-set */
	char szMapFileName[FILE_PATH_LENGTH];

	/*! 
	\brief Path to text file containing phenotypes of the sample array. 
	\sa sample.h
	*/
	char szPheFileName[FILE_PATH_LENGTH];

	/*! 
	\brief Path to the permutation index file. 
	\sa permfile.h
	*/
	char szPermFileName[FILE_PATH_LENGTH];

	/*! 
	\brief Path to the binary encoded sample array. 
	\sa sample.h
	*/
	char szSampleFileName[FILE_PATH_LENGTH];

	/*! 
	\brief Path to the binary encoded SNP list. 
	\sa bmapfile.h
	*/
	char szBmapFileName[FILE_PATH_LENGTH];

	/*! 
	\brief Path to the bit-string encoded, Individual-Major allelic data file. 
	\sa ipedfile.h
	*/
	char szIpedFileName[FILE_PATH_LENGTH];

	/*!
	\brief Path to the bit-string encoded, SNP-Major allelic data file. 
	\sa spedfile.h
	*/
	char szSpedFileName[FILE_PATH_LENGTH];

	/*! 
	\brief Path to the byte encoded, SNP-Major allelic data file. 
	\sa bpedfile.h
	*/
	char szBpedFileName[FILE_PATH_LENGTH];

	/*! 
	\brief Path to person exclusion list.
	\sa bfilter_parse_exclude_list()
	*/
	char szExcludeListFileName[FILE_PATH_LENGTH];

	/*! 
	\brief Path to person missingness file.
	\sa missingnessfile.h
	*/
	char szMissingnessFileName[FILE_PATH_LENGTH];

	/*! 
	\brief Path to a founder file.
	\sa foundersfile.h
	*/
	char szFoundersFileName[FILE_PATH_LENGTH];

	/*! 
	\brief Path to a random seed file.
	\sa seedfile.h
	*/
	char szSeedFileName[FILE_PATH_LENGTH];

	/*! 
	\brief Path to a strata or layer file for people in a genomic data-set.
	\sa solfile.h
	*/
	char szSolFileName[FILE_PATH_LENGTH];

	/*! 
	\brief Path to an file containing 'adjusted' p-values.
	\sa adjusted.h
	*/
	char szAdjustedFileName[FILE_PATH_LENGTH];

	/*! 
	\brief Sorting column for statistical output 
	\details default = \ref DEFAULT_SORT_COL_NAME 
	*/
	char szSortColumnName[TOKEN_LENGTH];

	/*! 
	\brief Response variable column in a text phenotype file 
	\details 1..n, default = \ref DEFAULT_MPHENO
	\sa sample.h
	*/
	int mpheno; 
	
	/*! 
	\brief Skip Family ID token when parsing a PED file 
	\details (default = FALSE)
	*/
	BOOL pedSkipFid;

	/*! 
	\brief Skip Parental ID token when parsing a PED file
	\details default = FALSE 
	*/
	BOOL pedSkipParents;

	/*! 
	\brief Skip gender token when parsing a PED file
	\details default = FALSE
	*/
	BOOL pedSkipSex;

	/*! 
	\brief Skip phenotype token when parsing a PED file 
	\details default = FALSE 
	*/
	BOOL pedSkipPhenotype;

	/*! \brief Missing genotype string. */
	char missing_genotype[TOKEN_LENGTH];

	/*! \brief Missing phenotype string. */
	char missing_phenotype[TOKEN_LENGTH];

	/*! 
	\brief Using alternate 0/1 phenotype coding scheme 
	\details default = FALSE
	*/
	BOOL coding01; // Phenotype coding scheme

	/*! 
	\brief Inputted PED has optional liability column 
	\details default = FALSE 
	*/
	BOOL liability;

	/*! 
	\brief Ignore missing gender codes 
	\details default = TRUE
	*/
	BOOL ignore_missing_sex;

	/*! \brief Preserve all genotypes. */
	BOOL preserve_all_genotypes;

	/*! \brief Phenotype is a quantitative trait. */
	BOOL qt;

	/*! \brief Phenotype is a binary trait. */
	BOOL bt; 

	/*! \brief Local working directory */
	char ldir[FILE_PATH_LENGTH];

	/*! \brief Remote working directory */
	char rdir[FILE_PATH_LENGTH];

	/*! 
	\brief Job/Run Identifier 
	\details default = 'run_' + system time in milleseconds */
	char szRunId[TOKEN_LENGTH];

	/*! 
	\brief Flag if data-set has non-founders 
	\details default = FALSE 
	*/
	BOOL has_nonfounders;

	/*! 
	\brief Flag if obliged to count missing phenotypes 
	\details default = FALSE 
	*/
	BOOL oblig_missing;

	/*! 
	\brief Flag if include non-founders in calculations
	\details default = FALSE
	*/
	BOOL summ_nonfounders;

	/*! 
	\brief Flag if calculation needs to do allelic frequency counts 
	\details default = FALSE 
	*/
	BOOL af_count;

	/*! 
	\brief Maximum inclusion threshold for Person missingness
	\details default = \ref DEFAULT_MIND 
	*/
	double MAX_IND_MISSING;

	/*! 
	\brief Maximum inclusion threshold for SNP missingness
	\details default = \ref DEFAULT_GENO 
	*/
	double MAX_GENO_MISSING;

	/*! 
	\brief Numerical precision for numerical solutions.
	\details default = 0.0 
	*/
	double epsilon; 

	/*! 
	\brief Minimum value for Major Allelic Frequency (MAF)
	\details default = \ref DEFAULT_MAF 
	*/
	double min_af;
	
	/*! 
	\brief Maximum value for Major Allelic Frequency (MAF)
	\details default = 1.0 
	*/
	double max_af;

	/*! 
	\brief Minimum CHISQ cell size 
	\details default = 5 
	*/
	int min_geno_cell;

	/*! 
	\brief No. permutations/replicates 
	\details default = \ref DEFAULT_NPERM 
	*/
	int nperm;

	/*! 
	\brief PMODEL Option - do genotypic calculation 
	\details default = FALSE
	*/
	BOOL model_perm_gen;
	
	/*! 
	\brief PMODEL Option - do dominant calculation 
	\details default = FALSE
	*/
	BOOL model_perm_dom;
	
	/*! 
	\brief PMODEL Option - do recessive calculation 
	\details default = FALSE
	*/
	BOOL model_perm_rec; 
	
	/*! 
	\brief PMODEL Option - do CAT-Trend test calculation 
	\details default = FALSE
	*/
	BOOL model_perm_trend;
	
	/*! 
	\brief PMODEL Option - do allelic calculation 
	\details default = FALSE
	*/
	BOOL model_perm_allelic;

	/*! 
	\brief Echo status messages to STDOUT 
	\details default = TRUE
	*/
	BOOL verbose; 

	/*!
	\brief P-value significance threshold */
	double pfvalue;

	/*!
	\brief Narrow epistasis significance threshold 
	\details 
		%Set to find interesting interactions<br> 
		default = \ref DEFAULT_EPI_ALPHA1
	*/
	double epi_alpha1;

	/*!
	\brief Wider epistasis significance threshold
	\details 
		%Set to find overall interaction counts with a particular SNP.<br>
		default = \ref DEFAULT_EPI_ALPHA1
	*/
	double epi_alpha2; 

	/*!
	\brief Randomisation threshold for epistasis permutation
	\details 
		Need to added this threshold to avoid bias in the epistasis result.<br>
		default = fabs(ltqnorm(0.95 / 2))
	\sa ltqnorm()
	*/
	double epi_alpha; //Display threshold & count threshold

	/*! 
	\brief X-chromosome coding scheme, undocumented PLINK artifact. 
	\details default = 1
	*/
	int xchr_model; 

	/*!
	\brief Add sex-effect column when building a design matrix. 
	\details default = TRUE
	*/
	BOOL sex_effect;

	/*! 
	\brief Include males only 
	\details default = FALSE
	\deprecated Replaced with inclusion list
	*/
	BOOL filter_males;

	/*! 
	\brief Include females only 
	\details default = FALSE
	\deprecated Replaced with inclusion list
	*/
	BOOL filter_females;

	/*! 
	\brief Include cases only
	\details default = FALSE
	\deprecated Replaced with inclusion list
	*/
	BOOL filter_cases;

	/*! 
	\brief Include control only 
	\details default = FALSE
	\deprecated Replaced with inclusion list
	*/
	BOOL filter_controls;

	/*! 
	\brief Apply no filtering toa genomic data-set
	\details default = FALSE
	\deprecated Not used
	*/
	BOOL no_filtering;

	
	/*! 
	\brief Filter SNPs with HWE method. 
	\details default = FALSE
	*/
	BOOL HWD_test;

	/*! 
	\brief Using standard HWE test. 
	\details default = TRUE
	*/
	BOOL HWD_standard;

	/*! 
	\brief Flag if QT so apply HWE on SNP probability only rather just to controls.
	\details default = TRUE
	*/
	BOOL HWD_filter_on_all;

	/*! 
	\brief HWE Inclusion threshold
	\details default = \ref DEFAULT_HWE_LIMIT
	*/
	double HWD_limit;

	/*! 
	\brief Log SNP removal (single-core filter option)
	\details default = FALSE
	\deprecated Not used
	*/
	BOOL logRemovals;

	/*! 
	\brief No. levels in a phenotype strata 
	\details default = 0
	*/
	int nk;

	/*!
	\brief Confidence Interval
	\details default = \ref DEFAULT_CI_LEVEL
	*/
	double ci_level;

	/*! 
	\brief Flag if using version 1 of CMH Algorithm.
	\details default = FALSE
	\sa pcmh_bt_assoc()
	*/
	BOOL CMH_test_1;
	
	/*! 
	\brief Flag if using version 2 of CMH Algorithm.
	\details default = FALSE
	\sa pcmh_bt_assoc()
	*/
	BOOL CMH_test_2;
	
	/*! 
	\brief Flag if want to add breslowday output to the CMH algorithm.
	\details default = FALSE
	\sa pcmh_bt_assoc()
	*/
	BOOL breslowday;

	/*!
	\brief Use Genomic control significance value when 'adjusting' p-values.
	\details default = FALSE
	\sa adjust.h
	*/
	BOOL use_GC; 

	/*!
	\brief Flag to constrain lamba value when 'adjusting' p-values.
	\details default = FALSE
	\sa adjust.h
	*/
	BOOL fix_lambda;

	/*!
	\brief A named column to read from a text phenotype file */
	char szPheColumnName[TOKEN_LENGTH];

	/*!
	\brief A named column to read from a text phenotype file for phenotype stratification */
	char szSolColumnName[TOKEN_LENGTH];

	/*!
	\brief Element window centered on a anchor SNP for LD sampling
	\details default = \ref DEFAULT_DISP_SNP_WINDOW
	*/
	int disp_r_window_snp;

	/*!
	\brief Kilobase window centered on a anchor SNP for LD sampling
	\details default = \ref DEFAULT_DISP_SNP_WINDOW_KB
	*/
	int disp_r_window_kb;
};

/*! 
\brief Initialise all the environmental/constant values to specified default values. 
\param ops Options Structure
\returns 1 on success, 0 on failure
*/
int options_init(struct selected_options *ops);

#ifdef __cplusplus
}
#endif

#endif  // _OPTIONS_H_
